{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f9072ae-4b23-4942-8e46-140081884238",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import make_classification\n",
    "import numpy as np\n",
    "\n",
    "# 生成二分类问题的虚拟数据\n",
    "X_binary, y_binary = make_classification(n_samples=1000, n_features=20, n_informative=2, n_redundant=10, n_classes=2, random_state=42)\n",
    "\n",
    "# 生成多分类问题的虚拟数据，调整 n_informative 的值\n",
    "X_multi, y_multi = make_classification(n_samples=1000, n_features=20, n_informative=3, n_redundant=10, n_classes=3, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fc745e6-c6a3-488c-9d8d-e04320c2cdf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "# 二分类问题数据划分\n",
    "X_binary_train, X_binary_test, y_binary_train, y_binary_test = train_test_split(X_binary, y_binary, test_size=0.2, random_state=42)\n",
    "\n",
    "# 多分类问题数据划分\n",
    "X_multi_train, X_multi_test, y_multi_train, y_multi_test = train_test_split(X_multi, y_multi, test_size=0.2, random_state=42)\n",
    "\n",
    "# 训练二分类模型\n",
    "log_reg_binary = LogisticRegression(max_iter=1000)\n",
    "log_reg_binary.fit(X_binary_train, y_binary_train)\n",
    "\n",
    "svm_binary = SVC(kernel='linear', probability=True)\n",
    "svm_binary.fit(X_binary_train, y_binary_train)\n",
    "\n",
    "# 训练多分类模型\n",
    "decision_tree_multi = DecisionTreeClassifier()\n",
    "decision_tree_multi.fit(X_multi_train, y_multi_train)\n",
    "\n",
    "random_forest_multi = RandomForestClassifier()\n",
    "random_forest_multi.fit(X_multi_train, y_multi_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9eddb002-8dbd-492d-ae09-40af88d0b440",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix, accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# 二分类模型预测\n",
    "y_binary_pred_log_reg = log_reg_binary.predict(X_binary_test)\n",
    "y_binary_pred_svm = svm_binary.predict(X_binary_test)\n",
    "\n",
    "# 多分类模型预测\n",
    "y_multi_pred_tree = decision_tree_multi.predict(X_multi_test)\n",
    "y_multi_pred_forest = random_forest_multi.predict(X_multi_test)\n",
    "\n",
    "# 计算混淆矩阵和精度\n",
    "def plot_confusion_matrix(y_true, y_pred, title):\n",
    "    cm = confusion_matrix(y_true, y_pred)\n",
    "    acc = accuracy_score(y_true, y_pred)\n",
    "    sns.heatmap(cm, annot=True, fmt='d')\n",
    "    plt.title(f'Confusion Matrix: {title} (Accuracy: {acc:.2f})')\n",
    "    plt.show()\n",
    "\n",
    "plot_confusion_matrix(y_binary_test, y_binary_pred_log_reg, 'Logistic Regression Binary')\n",
    "plot_confusion_matrix(y_binary_test, y_binary_pred_svm, 'SVM Binary')\n",
    "plot_confusion_matrix(y_multi_test, y_multi_pred_tree, 'Decision Tree Multi')\n",
    "plot_confusion_matrix(y_multi_test, y_multi_pred_forest, 'Random Forest Multi')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f50773a7-9480-4106-b652-e2f45d79b061",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import precision_recall_curve\n",
    "\n",
    "# 计算二分类模型的P-R曲线\n",
    "y_binary_prob_log_reg = log_reg_binary.predict_proba(X_binary_test)[:, 1]\n",
    "precision_log_reg, recall_log_reg, _ = precision_recall_curve(y_binary_test, y_binary_prob_log_reg)\n",
    "\n",
    "y_binary_prob_svm = svm_binary.predict_proba(X_binary_test)[:, 1]\n",
    "precision_svm, recall_svm, _ = precision_recall_curve(y_binary_test, y_binary_prob_svm)\n",
    "\n",
    "# 绘制P-R曲线\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(recall_log_reg, precision_log_reg, label='Logistic Regression')\n",
    "plt.plot(recall_svm, precision_svm, label='SVM')\n",
    "plt.xlabel('Recall')\n",
    "plt.ylabel('Precision')\n",
    "plt.title('Precision-Recall Curve')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89a25cc6-504e-4128-a29b-100339c2a5d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import f1_score, recall_score\n",
    "\n",
    "# 计算F1-Recall曲线\n",
    "def plot_f1_recall_curve(y_true, y_scores, title):\n",
    "    f1_scores = []\n",
    "    recall_scores = []\n",
    "    thresholds = np.arange(0, 1, 0.01)\n",
    "    for threshold in thresholds:\n",
    "        y_pred = (y_scores >= threshold).astype(int)\n",
    "        f1 = f1_score(y_true, y_pred)\n",
    "        recall = recall_score(y_true, y_pred, average='binary')  # 指定average参数\n",
    "        f1_scores.append(f1)\n",
    "        recall_scores.append(recall)\n",
    "    plt.plot(recall_scores, f1_scores)\n",
    "    plt.title(f'F1-Recall Curve: {title}')\n",
    "    plt.xlabel('Recall')\n",
    "    plt.ylabel('F1 Score')\n",
    "    plt.show()\n",
    "\n",
    "# 假设 y_binary_test, y_binary_prob_log_reg, 和 y_binary_prob_svm 已经定义\n",
    "# plot_f1_recall_curve(y_binary_test, y_binary_prob_log_reg, 'Logistic Regression')\n",
    "# plot_f1_recall_curve(y_binary_test, y_binary_prob_svm, 'SVM')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b30b6d00-8282-46fd-96c8-7502ce63f59d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import roc_curve, roc_auc_score\n",
    "\n",
    "# 计算ROC曲线和AUC\n",
    "fpr_log_reg, tpr_log_reg, _ = roc_curve(y_binary_test, y_binary_prob_log_reg)\n",
    "auc_log_reg = roc_auc_score(y_binary_test, y_binary_prob_log_reg)\n",
    "\n",
    "fpr_svm, tpr_svm, _ = roc_curve(y_binary_test, y_binary_prob_svm)\n",
    "auc_svm = roc_auc_score(y_binary_test, y_binary_prob_svm)\n",
    "\n",
    "# 绘制ROC曲线\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.plot(fpr_log_reg, tpr_log_reg, label=f'Logistic Regression (AUC: {auc_log_reg:.2f})')\n",
    "plt.plot(fpr_svm, tpr_svm, label=f'SVM (AUC: {auc_svm:.2f})')\n",
    "plt.plot([0, 1], [0, 1], 'k--')\n",
    "plt.xlabel('False Positive Rate')\n",
    "plt.ylabel('True Positive Rate')\n",
    "plt.title('ROC Curve')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
